package com.ophiux.member.controller;

import com.ophiux.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ophiux.common.core.base.Result;
import com.ophiux.member.model.dto.MemberDTO;
import com.ophiux.member.model.vo.MemberAddVo;
import com.ophiux.member.model.vo.MemberLoginReqVo;
import com.ophiux.member.model.vo.MemberQueryVo;
import com.ophiux.member.model.vo.MemberUpdateVo;
import com.ophiux.order.api.feign.IOrderFeignApi;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @desc:
 * @author: hhl
 * @date：2021/10/22 9:56
 */
@RestController
@RequestMapping("/member")
@Api(tags = "会员")
@RefreshScope
public class MemberController {
    @Autowired
    IMemberService memberService;
    @Autowired
    RestTemplate restTemplate;

    @Autowired
    IOrderFeignApi orderFeignApi;
    @Autowired
    LoadBalancerClient loadBalancerClient;


    @ApiOperation("1.登录/注册")
    @PostMapping("/login")
    public Result<MemberDTO> login(@RequestBody MemberLoginReqVo vo) {
        return memberService.login(vo);
    }

    @ApiOperation("2.列表记录")
    @GetMapping
    public Result<IPage<MemberDTO>> list(@RequestParam(required = false, defaultValue = "1") Integer current,
                                         @RequestParam(required = false, defaultValue = "10") Integer size, @RequestBody MemberQueryVo queryDTO) {
        IPage<MemberDTO> page = memberService.getPage(new Page<>(current, size), queryDTO);
        return Result.success(page);
    }

    @ApiOperation("3.新增")
    @PostMapping("/add")
    public Result add(@RequestBody MemberAddVo vo) {
        return memberService.insert(vo);
    }

    @ApiOperation("4.修改")
    @PutMapping("/update")
    public Result update(@RequestBody MemberUpdateVo vo) {
        return memberService.update(vo);
    }

    @GetMapping("/test")
    public String test() {
        return orderFeignApi.createOrder();
    }

    @GetMapping("/test2")
    public String test2() {
        // 通过spring cloud common中的负载均衡接口选取服务提供节点实现接口调用
        ServiceInstance serviceInstance = loadBalancerClient.choose("ophiux-order");
        String url = serviceInstance.getUri() + "/order/api/createOrder";
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        return "Invoke : " + url + ", return : " + result;
    }

    @GetMapping("/getUserById")
    public String getUserById(Integer userId) {
        return memberService.getUserById(1);
    }

    @GetMapping("/updateAmount")
    //方便测试用的get请求
    public Result updateAmount(@RequestParam("amount") Integer amount){
        return memberService.updateAmount(amount);
    }
    
    
    @Value("${name}")
    String param;

//    @Value("${seata.client.rm-report-success-enable}")
    String param2;
    
    @GetMapping("/test3")
    public String test3() {
    	return param+"========="+param2;
    }

}
